package com.pervasa.demo.re.impl.data;

import java.util.ArrayList;
import java.util.Calendar;

public abstract class Event {
	private String name;
	private long startTime;
	private boolean isTriggered;
	private ArrayList<Event> eventListeners = new ArrayList<Event>();
	private ArrayList<Rule> ruleListeners = new ArrayList<Rule>();
		
	protected Event(String name) {
		this.startTime = 0;
		this.name = name;
	}
	
	public String getName() {
		return this.name;
	}

	public boolean isTriggered() {
		return isTriggered;
	}
	
	protected void setTriggered(boolean isTriggered) {
		this.isTriggered = isTriggered;
		System.out.println("Event " + name + " is triggered as " + isTriggered);
		for (Event e : eventListeners) {
			e.evaluate(this);
		}
		for (Rule r : ruleListeners) {
			r.evaluate();
		}
	}

	public void update(boolean isTriggered) {
		startTime = Calendar.getInstance().getTimeInMillis();
		this.isTriggered = isTriggered;
	}

	public long getStartTime() {
		return this.startTime;
	}
	
	public void addEventListener(Event event) {
		eventListeners.add(event);
	}
	
	public void addRuleListeners(Rule rule) {
		ruleListeners.add(rule);
	}

	public abstract void evaluate(Object arg);
	public abstract String getExpression()	;
}
